<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <!-- 左侧导航 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧导航 -->
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" class="avatar" />
          用户名：
        </li>
        <li>{{ userInfo.nickName }}</li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isloginss">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" v-else @click="changeisShowlogin(true)">登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeixinloginAPI } from "@/request/api";
export default {
  methods: {
    ...mapMutations({
      changeisShowlogin: "showmodal/changeisShowlogin",
      changeislogin: "islogins/changeislogin",
      initUserInfo: "user/initUserInfo",
    }),
    ...mapActions({
      asyncshowtoast: "istoasts/asyncshowtoast",
      asyncchangeUser: "user/asyncchangeUser",
    }),
  },
  created() {
    this.$router.onReady(async () => {
      let pathCode = this.$route.query.code;
      if (pathCode) {
        //发起微信扫码登录请求
        let res = await WeixinloginAPI({
          code: pathCode,
        });
        //分三种情况处理(拿到数据就OK了)
        // console.log("***res", res);
        if (res.code == 0) {
          //登录成功
          //1.提示成功
          this.asyncshowtoast({
            istoast: true,
            title: "微信扫码登录成功",
            type: "success",
          });
          //2.存储token
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          //3.更新登录状态
          this.changeislogin(true);
          //4.清除地址栏code
          this.$router.push(this.$route.path);
          //更新用户信息
          this.asyncchangeUser();
        } else if (res.code == 400) {
          //1.提示用户重新扫码
          this.asyncshowtoast({
            istoast: true,
            title: res.message,
            type: "danger",
          });
          //2.自动打开登录框
          this.changeisShowlogin(true);
        } else if (res.code == 407) {
          //用户第一次使用微信扫码(还没有和手机号关联起来)
          //1.提示用户进行一次手机号登录
          this.asyncshowtoast({
            istoast: true,
            title: res.message,
            type: "danger",
          });
          //2.自动打开登录框
          this.changeisShowlogin(true);
          //3.存储uuid,为了login.vue再使用进行关联
          localStorage.setItem("uuid", res.uuid);
          //4.清除地址栏中code(为了避免刷新又重新发起请求,此处将code删除)
          this.$router.push(this.$route.path);
        }
      } else {
        //需求:手动删除tiken后,切换页面时,需要更新登录状态
        //切换页面时 路由地址会发生变化
        let token = localStorage.getItem("x-auth-token");
        this.changeislogin(Boolean(token));
        if (token) {
          //已登录
          this.asyncchangeUser();
        } else {
          //未登录
          this.initUserInfo();
        }
      }
    });
  },
  computed: {
    ...mapState({
      isloginss: (state) => state.islogins.islogin,
      userInfo: (state) => state.user.userInfo,
      cartTotal: (state) => state.user.cartTotal,
    }),
  },
  /*   watch: {
    //需求:手动删除tiken后,切换页面时,需要更新登录状态
    //切换页面时 路由地址回发送变化
    //watch 监听数据变化
    "$route.path": {
      handler(newval, olval) {
        //更新登录状态,获取本地token 如果有 已登录 否则 未登录
        let token = localStorage.getItem("x-auth-token");
        if(token){
          this.changeislogin(true);
        }else{
          this.changeislogin(false);
        }
        //优化
        this.changeislogin(Boolean(token));
      },
    },
  }, */
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  line-height: 40px;
  color: #fffefe;
  font-size: 14px;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      li {
        float: left;
        margin-left: 20px;
        text-align: center;
        &.login-btn {
          width: 124px;
          background: #0a328e;
        }
      }
      .avatar {
        width: 23px;
        height: 22px;
        border-radius: 13px;
        vertical-align: middle;
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>